package com.ltsznh.android.anti_villain;

import android.app.TaskStackBuilder;
import android.content.Context;
import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by 涛 on 2014/7/29.
 */
public class ReadLog extends Thread {
    private Context context;
    private int logCount;

    private static final String TAG = "LogInfo:";

    private static class CallViewState{
        public static final String FORE_GROUND_CALL_STATE = "mForeground";

    }

    private static class CallState {
        public static final String DIALING = "DIALING";
        public static final String ALERTING = "C";
        public static final String ACTIVE = "ACTIVE";
        public static final String IDLE = "IDLE";
        public static final String DISCONNECTED = "DISCONNECTED";
    }

    public ReadLog(Context ctx){
        context = ctx;
    }

    public void run(){
        Log.v(TAG, "开始读取日志");

        String[] catchParams = {"logcat","-s"};
        String[] clearParams = {"logcat","-c"};

        try{
            Process process = Runtime.getRuntime().exec(catchParams);
            InputStream is = process.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));

            String line = null;
            while ((line = reader.readLine()) != null){
                logCount++;
                Log.w(TAG,line);

                if(logCount > 512){
                    Runtime.getRuntime().exec(clearParams).destroy();
                    logCount = 0;
                    Log.v(TAG,"===================清理日志=======================");

                    if(line.contains(CallViewState.FORE_GROUND_CALL_STATE) && line.contains(CallState.IDLE)){
                        Log.d(TAG, CallState.IDLE);
                        break;
                    }

                    if(line.contains(CallViewState.FORE_GROUND_CALL_STATE) && line.contains(CallState.DIALING)){
                        Log.d(TAG, CallState.DIALING);
                        break;
                    }

                    if(line.contains(CallViewState.FORE_GROUND_CALL_STATE) && line.contains(CallState.ALERTING)){
                        Log.d(TAG, CallState.ALERTING);
                        break;
                    }

                    if(line.contains(CallViewState.FORE_GROUND_CALL_STATE) && line.contains(CallState.DIALING)){
                        Log.d(TAG, CallState.ACTIVE);
                        break;
                    }

                    if(line.contains(CallViewState.FORE_GROUND_CALL_STATE) && line.contains(CallState.DISCONNECTED)){
                        Log.d(TAG, CallState.DISCONNECTED);
                        break;
                    }
                }
            }
            Log.v(TAG,"读取结束" + line);
        }catch (Exception e){
            e.printStackTrace();
            Log.v(TAG,"读取报错");
        }
    }
}